package com.lq.email.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lq.email.entity.EmailEntity;
import com.lq.email.entity.UserEmail;
import com.lq.email.exception.MyException;
import com.lq.email.mapper.EmailEntityMapper;
import com.lq.email.mapper.UserEmailMapper;
import com.lq.email.service.EmailService;
import com.lq.email.util.MyJavaMailSender;
import com.lq.email.util.MyUuid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @ClassName EmailServiceImpl
 * @Description
 * @Author LQ
 * @Date 2021/10/5 18:32
 * @Version 1.0
 */
@Service
public class EmailServiceImpl implements EmailService {
    @Autowired
    private UserEmailMapper userEmailMapper;

    @Autowired
    private MyJavaMailSender myJavaMailSender;
    @Autowired
    private EmailEntityMapper entityMapper;

    @Autowired
    JdbcTemplate template;

    @Override
    public void sendEmail(EmailEntity emailEntity) throws MyException {
        String userId = emailEntity.getUserId();
        if (StringUtils.isBlank(userId)) {
            throw new MyException("用户id不能为空");
        }
        QueryWrapper<UserEmail> warpper = new QueryWrapper<>();
        warpper.eq("userid", userId);
        UserEmail userEmail = userEmailMapper.selectOne(warpper);
        if (StringUtils.isBlank(emailEntity.getSendTo())) {
            emailEntity.setSendTo(userEmail.getEmailSendTo());
        }
        if (StringUtils.isBlank(emailEntity.getSendForm())) {
            emailEntity.setSendForm(userEmail.getEmaileSendForm());
        }
        JavaMailSenderImpl javaMailSender = myJavaMailSender.getJavaMailSender(userEmail);
        SimpleMailMessage message = new SimpleMailMessage();
        javaMailSender.setUsername(emailEntity.getSendForm());
        message.setFrom(javaMailSender.getUsername());
        message.setTo(emailEntity.getSendTo());

        // 设置主题
        message.setSubject(emailEntity.getSubject());
        // 设置邮件正文
        message.setText(emailEntity.getText());
        // 设置抄送人(可以多个)
        // message.setCc("37xxxxx37@qq.com");
        // 设置秘密抄送人(可以多个)
        // message.setBcc("14xxxxx098@qq.com");
        // 设置发邮件日期
        emailEntity.setSendTime(new Date());
        message.setSentDate(emailEntity.getSendTime());
        javaMailSender.send(message);
        emailEntity.setStatus(1);
        if (StringUtils.isBlank(emailEntity.getId())) {
            // 生成id
            emailEntity.setId(MyUuid.getUuid());
            entityMapper.insert(emailEntity);
        } else {
            QueryWrapper<EmailEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", emailEntity.getId());
            Integer emailEntityCount = entityMapper.selectCount(queryWrapper);
            if (emailEntityCount != null && emailEntityCount > 0) {
                entityMapper.updateByPrimaryKey(emailEntity);
            } else {
                entityMapper.insert(emailEntity);
            }
        }
    }
}
